package lib.bone.oss.service.impl;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.crypto.SecureUtil;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.oss.constant.FileResponseCodeConst;
import lib.bone.oss.constant.FileServiceNameConst;
import lib.bone.oss.dao.FileDAO;
import lib.bone.oss.domain.entity.FileEntity;
import lib.bone.oss.service.IFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @author: kindear
 */
@Slf4j
@Service(FileServiceNameConst.LOCAL)
public class FileServiceLocal implements IFileService {
    @Autowired
    FileDAO fileDAO;


    @Value("${file-upload-service.path}")
    private String fileParentPath;

    @Override
    public ResponseDTO<FileEntity> fileUpload(MultipartFile multipartFile, String path,String fileName) {
        FileEntity fileEntity = new FileEntity();
        fileEntity.setFileLocation(FileServiceNameConst.LOCAL);
        //判断文件是否为空
        if(Validator.isEmpty(multipartFile)){
            return ResponseDTO.wrap(FileResponseCodeConst.FILE_EMPTY);
        }

        if(Validator.isEmpty(fileParentPath)){
            return ResponseDTO.wrap(FileResponseCodeConst.LOCAL_UPDATE_PREFIX_ERROR);
        }
        // 获取文件大小
        fileEntity.setFileSize(multipartFile.getSize());
        // 获取文件名称
        if (Validator.isEmpty(fileName)){
            fileName = multipartFile.getOriginalFilename();
        }
        fileEntity.setFileName(fileName);


        // 计算文件路径
        StringBuilder builder = new StringBuilder(fileParentPath);
        builder.append(path);
        builder.append(File.separator);
        builder.append(fileName);
        String filePath = builder.toString();
        fileEntity.setFilePath(filePath);
        //计算文件唯一Hash
        // 存储文件 -- 创建对应文件
        FileUtil.touch(filePath);
        fileEntity.setFilePath(filePath);
        try {
            //计算文件MD5
            String uniCode = SecureUtil.md5(multipartFile.getInputStream());
            fileEntity.setUniCode(uniCode);

            //获取文件类型
            File file = FileUtil.file(filePath);
            multipartFile.transferTo(file);
            String fileType = FileTypeUtil.getType(file);
            fileEntity.setFileType(fileType);
        } catch (IOException e) {
            log.error("", e);
            return ResponseDTO.wrap(FileResponseCodeConst.UPLOAD_ERROR);
        }

        return ResponseDTO.succData(fileDAO.save(fileEntity));
    }


    @Override
    public ResponseDTO<String> getFileUrl(String path) {
        return null;
    }

    @Override
    public ResponseDTO<FileEntity> fileUpload(MultipartFile multipartFile, String path) {
        // 内部调用
        return fileUpload(multipartFile,path,null);
    }

    @Override
    public ResponseEntity<byte[]> fileDownload(String filePath, String fileName, HttpServletRequest request) {
        File file = new File(filePath);
        //File file1 = FileUtil.file(filePath);
        return this.downloadMethod(file, request);
    }
}
